﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace KingerRobot.MexicoSailunMDC.SpiService;

public class POTcmGtReqRepositories: IPOTcmGtReqRepositories
{
    /// <summary>
    /// 写入实体数据
    /// </summary>
    /// <param name="entity">博文实体类</param>
    /// <returns>插入个数 插入成功为1</returns>
    public int Add(POTcmGtReq entity)
    {
        var _db = Db.Default;
        return _db.TcmGtReqs.Insert(entity);
    }

    /// <summary>
    /// 批量插入实体
    /// </summary>
    /// <param name="listEntity">实体集合</param>
    /// <returns>影响行数</returns>
    public int Add(IEnumerable<POTcmGtReq> listEntity)
    {
        var _db = Db.Default;
        _db.TcmGtReqs.AddRange(listEntity);
        int insertedCount = _db.SaveChanges();
        return insertedCount;
    }

    /// <summary>
    /// 待条件更新所有数据
    /// </summary>
    /// <param name="whereExpression"></param>
    /// <param name="entity"></param>
    /// <returns></returns>
    public int Update(Expression<Func<POTcmGtReq, bool>> whereExpression, POTcmGtReq entity)
    {
        var _db = Db.Default;
        return _db.TcmGtReqs.Where(whereExpression).Update(new POTcmGtReq()
        {
            ID=entity.ID,
            TcmNo = entity.TcmNo,
            TcmDesc = entity.TcmDesc,
            TcmDir = entity.TcmDir,
            LdGt = entity.LdGt,
            CvGt = entity.CvGt,
            GtReq =entity.GtReq,
            RegDt = entity.RegDt,
            SulfopenTime = entity.SulfopenTime
        });
    }

    /// <summary>
    /// 查询第一条数据
    /// </summary>
    /// <param name="whereExpression"></param>
    /// <returns></returns>
    public POTcmGtReq First(Expression<Func<POTcmGtReq, bool>> whereExpression)
    {
        var _db = Db.Default;
        if (whereExpression == null) return null;
        return _db.TcmGtReqs.Where(whereExpression).FirstOrDefault();
    }
}
